﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.NodeFactory;
using SPCFMedicineInformer.Constants;
using Vega.USiteBuilder;
using SPCFMedicineInformer.DocumentTypes;
using umbraco.cms.businesslogic.web;
using umbraco.BusinessLogic;
using SPCFMedicineInformer.Helpers;

namespace SPCFMedicineInformer.Masterpages
{
    public partial class Admin_EmailTemplate : System.Web.UI.MasterPage
    {
        int id;
        
        protected void Page_Init(object sender, EventArgs e)
        {
            ((umbraco.UmbracoDefault)this.Page).ValidateRequest = false;

        }
        protected void Page_Load(object sender, EventArgs e)
        {

            var node = NodeLocator.GetNodeOfExactType<EmailTemplates>();
            if (!IsPostBack)
            {
                if (node != null)
                {
                    id = node.Id;
                    taLeftContent.Value = node.ContactUsTemplate;
                    //taRightContent.Value = node.RegistrationTemplate;
                }
            }
        }
        void UpdateNode()
        {
            var node = NodeLocator.GetNodeOfExactType<EmailTemplates>();
            if (node != null)
            {
                var doc = new Document(node.Id);
                TransferDetailsToUmbraco(doc);
            }
            
            
        }
        void TransferDetailsToUmbraco(Document doc)
        {
            doc.getProperty(UmbracoPropertyName.EMAIL_TEMPLATES_CONTACT_US).Value = taLeftContent.Value;
            //doc.getProperty(UmbracoPropertyName.EMAIL_TEMPLATES_REGISTRATION).Value = taRightContent.Value;
            doc.Publish(User.GetUser(0));
            umbraco.library.UpdateDocumentCache(doc.Id);
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            UpdateNode();
            FormHelper.ShowMessage(MessageType.Done, "Email Templates Succesfully Saved!", MsgLiteral);
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/admin/utilities/");
        }
    }
}